<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/12
 * Time: 13:49
 */

namespace Admin\Controller;


use Common\Controller\AuthController;
use OT\TagLib\Think;

class TableController extends AuthController {

    public function send_table(){
        $obj = D('B2c');
        $company = $obj->table('tb_company')->field('comid,name')->select();
        $condition['classify'] = 2;
        $type = $obj->table('xf_table_type')->where($condition)->select();
        $data_year = date('Y')+5;
        $year = [];
        for ($i = 0; $i < 11 ; $i++) {
            $year[] = $data_year - $i;
        }
        $this->assign('year',$year);
        $this->assign('type',$type);
        $this->assign('company',$company);
        $this->display();

    }

    public function send_table_run(){

        if (!IS_AJAX){
            $this->error('提交方式不正确',0,0);
        }

        $id = I('tableid');
        $year = I('year');
        $quarter = I('quarter');
        $endtime = strtotime(I('endtime'));
        $user_arr = I('sendid');
        $obj = D('B2c');
        $table_info = $obj->table('xf_table_type')->where(array('id'=>$id))->find();
        $install_info = strval($table_info['install_info']);

        $num = 0;
        foreach ($user_arr as $k=>$v){
            $con = array(
                'table_id' => $id,
                'quarter' => $quarter,
                'year' => $year,
                'ComID' => $v
            );

            $isset = $obj->table('xf_total')->where($con)->select();

            if (!$isset){
                $companyname = $obj->table('tb_company')->where(array('ComID'=>$v))->field('name')->find();
                $data['quarter'] = $quarter;
                $data['year'] = $year;
                $data['install_info'] = $install_info;
                $data['ComID'] = $v;
                $data['is_write'] = 0;
                $data['endtime'] = $endtime;
                $data['title'] = $table_info['title'];
                $data['code'] = $table_info['code'];
                $data['target'] = $companyname['name'];
                $data['table_id'] = $id;
                $data['remark'] = $table_info['remark'];
                $data['state'] = 0;
                $obj->table('xf_total')->add($data);
                $num++;
            }

        }

        if ($num !=0){
            $this->success('成功下发'.$num.'条记录',U('table/table_list'),0);
        }else{
            $this->error('报表不能重复推送',U('table/table_list'),0);
        }



    }
    //筛选企业分类
    public function type_ajax(){
        if (!IS_AJAX){
            $this->error('提交方式不正确',0,0);
        }
        $id = I('id');
        $obj = D('B2c');
        $res = $obj->table('tb_company')->where(array('type'=>$id))->field('comid,name')->select();

        $this->ajaxReturn($res);
    }

    public function table_del(){
        $id = I('id');
        $p = I('p')?I('p'):1;
        $obj = D('B2c');
        if ($id && is_numeric($id)){
            $res = $obj->table('xf_total')->where(array('id'=>$id))->delete();
            if ($res){
                $this->redirect('table_list', array('p' => $p));
            }
        }
    }

    public function company_ajax(){
        if (!IS_AJAX){
            $this->error('提交方式不正确',0,0);
        }
        $text = I('text');
        $obj = D('B2c');
        $con = array(
            'Name'=> array('like','%'.$text.'%'),
        );
        $res = $obj->table('tb_company')->where($con)->field('comid,name')->select();

        $this->ajaxReturn($res);


    }

    public function table_list(){

        $obj = D('B2c');
        $p = I('p')?I('p'):1;
        $table_id = I('t');
        $year = I('y');
        $quarter = I('q');
        $company = I('com');
        $table_state = I('status');
        $con1 = array(
            'Name'=>array('like','%'.$company.'%')
        );
        $com = $obj->table('tb_company')->where($con1)->field('comid')->select();
        foreach ($com as $v){
            $com1[] = $v['comid'];
        }
        if (!empty($company)){
            if (!empty($com1)){
                $condition['ComID'] = array('in',$com1);
            }else{
                $condition['ComID'] = 0;
            }
        }
        if ($table_state!=''){
            if ($table_state == 4){
                $condition['state'] = 0;
                $condition['endtime'] = ['lt',time()];
            }elseif ($table_state == 0){
                $condition['state'] = 0;
                $condition['endtime'] = ['egt',time()];
            }else{
                $condition['state'] = $table_state;
            }
        }else{
            $table_state = 5;
        }
        if (!empty($table_id)){
            $condition['table_id'] = $table_id;
        }
        if (!empty($quarter)){
            $condition['quarter'] = $quarter;
        }
        if (!empty($year)){
            $condition['year'] = $year;
        }
        $count = $obj->table('xf_total')->where($condition)->count();
        $Page = new \Think\Page($count,20);
        $show = $Page->show();
        $res = $obj->table('xf_total')->where($condition)->order('id desc')->page($p,20)->select();
        $nowtime = time();
        foreach ($res as $k=>$v){

            $comid = $v['comid'];
            $u_name = $obj->table('tb_company')->where(array('ComID'=>$comid))->field('name')->find();

            $res[$k]['u_name'] = $u_name['name'];

        }
        $tableid =$obj->table('xf_table_type')->where(array('classify'=>2))->field('id,title')->select();

        $this->assign('res',$res);
        $this->assign('show',$show);
        $this->assign('year',$year);
        $this->assign('tid',$table_id);
        $this->assign('nowtime',$nowtime);
        $this->assign('quarter',$quarter);
        $this->assign('tableid',$tableid);
        $this->assign('company',$company);
        $this->assign('table_state',$table_state);
        $this->display();

    }

    public function table_info(){

        $id = I('id');
        $obj = D('B2c');
        $data = $obj->table('xf_total')->where(array('id'=>$id))->find();
        $res = json_decode(trim($data['install_info']),true);
        $this->assign('res',$res);
        $this->assign('info',$data);
        $this->display();

    }

    public function time_edit(){
        if (!IS_AJAX){
            $this->error('提交方式有误',0,0);
        }
        $obj= D('B2c');
        $id = I('id');
        $endtime = strtotime(I('endtime'));
        $res = $obj->table('xf_total')->where(array('id'=>$id))->setField('endtime',$endtime);
        if ($res){
            $this->success('重置成功',U('table_list'),0);
        }
    }

    public function reset_time(){
        $id = I('id');
        $obj = D('B2c');
        $res = $obj->table('xf_total')->where(array('id'=>$id))->field('id,endtime')->find();
        if ($res){
            $res['status'] = 1;
            $res['endtime'] = date('Y-m-d H:i:s',$res['endtime']);
        }
        $this->ajaxReturn($res);
    }

    public function table_offline_run(){

        if (!IS_AJAX){
            $this->error('提交方式不正确',0,0);
        }

        $id = I('tableid');
        if ($_FILES){
            $upload = New \Think\Upload();
            $upload->maxSize = 10485760;// 设置附件上传大小
            $upload->exts = array('xls','xlsx');// 设置附件上传类型
            $upload->rootPath = './Public/uploads/excel/'; // 设置附件上传根目录
            $upload->savePath = ''; // 设置附件上传（子）目录
            $upload->saveRule = 'time';
            if (!file_exists($upload->rootPath)){
                mkdir($upload->rootPath,0777);
            }
            $info = $upload->upload();

            if ($info) {
                $excel_url = '/uploads/excel/' . $info['excel']['savepath'] . $info['excel']['savename'];//如果上传成功则完成路径拼接
            }else{
                $this->error('上传文件失败');
            }
            $fileurl =  './Public'.$excel_url;
            $data = add_exl($fileurl);
            $count1 = count($data);
            $i = 0;
            foreach ($data as $k=>$v){
                $arr[$k]['name']=$v[$i];
                $arr[$k]['metering']=$v[$i+1];
                $arr[$k]['code']=$v[$i+2];
                $arr[$k]['last']=$v[$i+3];
                $arr[$k]['now']=$v[$i+4];
            }
            $obj = D('B2c');
            $olddata = $obj->table('xf_total')->where(array('id'=>$id))->field('finnal_info')->find();
            $oldarr = json_decode(strval(trim($olddata['finnal_info'])),true);
            $count2 = count($oldarr)+1;
            if ($count1 != $count2){
                $this->error('请提交原excel',0,0);
            }
            $newinfo_last = array();
            $newinfo_now = array();
            foreach ($arr as $k=>$v){
                foreach ($v as $key=>$value){
                    if ($key == 'last'){
                        array_push($newinfo_last,$value);
                    }elseif ($key == 'now'){
                        array_push($newinfo_now,$value);
                    }
                }
            }

            foreach ($oldarr as $key=>$value){
                foreach ($value as $k=>$v){

                    if ($k == 'last'){
                        $oldarr[$key][$k] = $newinfo_last[$i];
                    }elseif ($k == 'now'){
                        $oldarr[$key][$k] = $newinfo_now[$i];
                    }

                }
                $i++;
            }
            /*print_r($oldarr);die;*/
            $newstr = json_encode($oldarr);
            $res = $obj->table('xf_total')->where(array('id'=>$id))->setField(array('finnal_info'=>$newstr));
            if ($res){
                $this->success('修改成功',U('table_list'),0);
            }
        }

    }

    public function table_edit(){

        $id = I('id');
        $obj = D('B2c');
        $data = $obj->table('xf_total')->where(array('id'=>$id))->find();
        $u_name = $obj->table('tb_company')->where(array('ComID'=>$data['comid']))->field('name')->find();
        $res = json_decode(strval(trim($data['finnal_info'])),true);
        $oldjson = strval(trim($data['finnal_info']));
        //先进类别
        $xj_type = [0=>['name'=>'高性能纤维及复合材料'],1=>['name'=>'新型金属功能材料'],2=>['name'=>'新型能源材料'],3=>['name'=>'绿色建筑材料(建筑工业化部品部件)'],4=>['name'=>'3D打印材料(增材制造)']];
        //智能类别
        $zn_type = [0=>['name'=>'新能源装置装备'],1=>['name'=>'新型商用车'],2=>['name'=>'节能环保装备'],3=>['name'=>'智能终端'],4=>['name'=>'大数据']];
        $city = M('city_list')->select();
        $this->assign('zn_type',$zn_type);
        $this->assign('xj_type',$xj_type);
        $country = M('country_list')->select();
        $goods = M('goods_catgory')->select();
        $this->assign('city',$city);
        $this->assign('country',$country);
        $this->assign('goods',$goods);
        $this->assign('hphp',$oldjson);
        $this->assign('res',$res);
        $this->assign('info',$data);
        $this->assign('u_name',$u_name['name']);
        $this->display();

    }

    public function table_edit_run(){

        if (!IS_AJAX){
            $this->error('提交方式不正确',0,0);
        }

        $id = I('tableid');
        $obj = D('B2c');
        $olddata = $obj->table('xf_total')->where(array('id'=>$id))->field('finnal_info')->find();
        $oldarr = json_decode(strval(trim($olddata['finnal_info'])),true);

        $newlast = I('last');
        $newnow = I('now');
        //foreach 定义自增变量
        $i = 0;
        //对相应的数据进行更新
        foreach ($oldarr as $key=>$value){
            foreach ($value as $k=>$v){

                if ($k == 'last'){
                    $oldarr[$key][$k] = $newlast[$i];
                }elseif ($k == 'now'){
                    $oldarr[$key][$k] = $newnow[$i];
                }

            }
            $i++;


        }
        $newstr = json_encode($oldarr);
        $res = $obj->table('xf_total')->where(array('id'=>$id))->setField(array('finnal_info'=>$newstr));
        if ($res){
            $this->success('修改成功',U('table_list'),0);
        }

    }

    public function table_offline(){

        $id = I('id');
        $obj = D('B2c');
        $data = $obj->table('xf_total')->where(array('id'=>$id))->find();
        $u_name = $obj->table('tb_company')->where(array('ComID'=>$data['comid']))->field('name')->find();
        $res = json_decode(trim($data['finnal_info']),true);
        $this->assign('u_name',$u_name['name']);
        $this->assign('res',$res);
        $this->assign('info',$data);
        $this->display();

    }

    public function table_download(){

        $id = I('id');
        $obj = D('B2c');
        $data = $obj->table('xf_total')->where(array('id'=>$id))->find();
        //生成导出数据的数组
        $res = json_decode(trim($data['finnal_info']),true);
        //季度
        $quarter = $data['quarter'];
        $title = $data['title'];
        $filename = $data['title'].date("Ymd",time()).rand(11,99);
        upload_exl($res,$filename,$quarter,$title);

    }

    public function install_table(){

        $obj = D('B2c');
        $table = $obj->table('xf_table_type')->order('code asc')->select();
        $this->assign('table',$table);
        $this->display();

    }

    public function install_table_info(){

        $id = I('id');
        $obj = D('B2c');
        $data = $obj->table('xf_table_type')->where(array('id'=>$id))->find();

        $res = json_decode(trim($data['install_info']),true);

        $this->assign('res',$res);
        $this->assign('info',$data);
        $this->display('table_info');

    }


}